library(randomForest)
library(gbm)
library(e1071)
library(imputation)
library(mlbench)
library(ada)
library(adabag)
library(rpart)

trainData = read.csv("train.csv", sep=' ', header=FALSE)
trainData$V3[trainData$V3 == "NaN"] = NA
train = read.csv("train.csv", sep=' ', header=FALSE)

testData = read.csv("test.csv", sep=' ', header=FALSE)
testData$V3[testData$V3 == "NaN"] = NA
testData$V9[testData$V9 == "NaN"] = NA
testData = kNNImpute(testData, k = 4, verbose=F)$x

trainData = kNNImpute(trainData, k = 4, verbose=F)$x
#trainData$V13 <- NULL

#pcaData <- rbind(trainData, testData)
luckyData = read.csv("submission9.csv", header=TRUE)
a <- princomp(~ .,trainData)

tr = trainData
tr$V13 <- NULL
pca <- princomp(tr)
tr <- cbind(pca$scores[, 1:4], trainData$V13)
colnames(tr) <- c("V1", "V2", "V3", "V4", "class")

tr <- as.data.frame(tr)
split <- runif(dim(tr)[1]) > 0.5  
train <- tr[split,]
test <- tr[!split,]

svm1 <- svm(as.factor(class) ~ ., data=train, kernel="radial", gamma=10)
gbm1 <- gbm(class ~ ., data=train, distribution="bernoulli", n.trees=1000, interaction.depth=5, shrinkage=0.02, bag.fraction=0.65)
rf <- randomForest(as.factor(class) ~ ., train, n.tree=10000)

predict_rf <- predict(rf, test, type="response")
write.csv(predict_rf, "predictions_rf.txt")
predictions_rf <- read.csv("predictions_rf.txt")

predict_gbt <- predict(gbm1, test, n.trees=1000, type="response")
predictions_gbt <- round(predict_gbt)

predict_svm <- predict(svm1, test)
write.csv(predict_svm, "predictions_svm.txt")
predictions_svm <- read.csv("predictions_svm.txt")

all_predictions <- c(nrow(test))
sum <- predictions_rf["x"] + predictions_gbt + predictions_svm["x"]
count <- 0
for (i in 1:nrow(test))
{
  if (sum[i, "x"] <2)
  {
    all_predictions[i] <- 0
  } 
  else
  {
    if (sum[i, "x"] == 2) 
    {
      all_predictions[i] <- predictions_rf[i, "x"]
    }
    else
    {
      all_predictions[i] <- 1
    }
  }
  
  #if (all_predictions[i] != luckyData[i, "Category"])
  #{
  #  print(i)
  #}
}

table1 <- table(test$class, all_predictions )
rss <- (table1[1,1] + table1[2,2])/sum(table1)
print(paste("rss = ", rss))
print(table1)
